package com.example.demo;

import com.example.demo.HashMap_put.HashMap;
import org.springframework.util.Assert;

import java.util.Objects;

/**
 *
 * @author sunny
 */
public class NodePractice {

    static class Node{
        public int value;
        public Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
        public Node(int value) {
            this.value = value;

        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return value == node.value &&
                    Objects.equals(next, node.next);
        }

        @Override
        public int hashCode() {
            return Objects.hash(value, next);
        }


    }

    public static void main(String[] args) {
        Node node = getNode();
        Node node1 = delete(node);
        print(node1);
    }
    /**
     * 1->2-2>3->->4->4->5-5
     */
    /// pre->1 1
    public static Node delete(Node head){
        if(head ==null || head.next ==null){
            return head;
        }
        Node newHead = new Node(-1);
        Node cur = head;
        Node pre = newHead;
        int i = 1;
        while(cur!=null){
            if(cur.next!=null&&cur.next.value==cur.value){
                while(cur.next!=null&&cur.next.value==cur.value){
                    cur = cur.next;
                }
                cur = cur.next;
                newHead.next = cur;
            }else {
                // 1-> 3
                if(i==1){
                    newHead.next = cur;

                }

                newHead = cur;

                cur = cur.next;
            }
            i++;
        }
        return pre.next;

    }
    /**
     * 1->2-2>3->3->4->4->5-5
     */


    public static Node getNode(){

        Node head11 = new Node(5,null);
        Node head10 = new Node(5,head11);
        Node head9 = new Node(4,head10);
        Node head8 = new Node(4,head9);
        Node head5 = new Node(3,head8);
        Node head3 = new Node(2,head5);
        Node head12 = new Node(2,head3);
        Node head1 = new Node(2,head12);
        Node head2 = new Node(2,head1);
        Node head= new Node(1,head2);

        return  head;
    }

    public static void print(Node head){
        while(head!=null){
            System.out.println(head.value);
            head = head.next;
        }
    }


    /**
     * 在一个排序的链表中，存在重复的结点，请重复的结点不保留，返回链表头指针，删除该链表中重复的结点，。 例如，
     * list
     * 链表1->2->2->2->2->3->3->4->4->5 处理后为 1->2->5
     */
    public static Node deleteDuplication(Node pHead)
    {
        if(pHead == null || pHead.next==null){
            return pHead;
        }
        Node head = null;
        Node pre = null;
        while(pHead!=null){
            if(pHead.next!=null&&pHead.value==pHead.next.value){
                while(pHead.next!=null&&pHead.value==pHead.next.value){
                    pHead = pHead.next;
                }
                pHead = pHead.next;
                pre.next = pHead;
            }else {
                if(head == null){
                    head = pHead;
                }
                if(pre == null){
                   // pre = pHead;
                    pre = head.next;
                }
                pHead = pHead.next;
            }
        }
        return head;
    }

   // 链表1->2->2->3->4->4->5 处理后为 1->2->5
    public static Node deleteDuplication1(Node pHead){
        Node newHead = new Node(-1);
        Node prev = newHead; // 虚拟节点
        Node cur = pHead; // 当前节点
        while(cur != null) {
            if (cur.next != null && cur.value == cur.next.value) {
                while (cur.next != null && cur.value == cur.next.value) {
                    cur = cur.next;
                }
                cur = cur.next;
                newHead.next = cur;
            } else {
                newHead.next = cur;
                newHead = cur;
                cur = cur.next;
            }

        }
        return prev.next;
//        return null;
    }


    /**
     * 在一个排序的链表中，存在重复的结点，请重复的结点不保留，返回链表头指针，删除该链表中重复的结点，。 例如，
     * list
     * 链表1->2->2->2->2->3->3->4->4->5->6 处理后为 1->2->5
     */
    public static Node deleteDuplication3(Node pHead){
        if(pHead==null || pHead.next ==null){
            return pHead;
        }
        Node newHead = new Node(-1);
        Node pre = newHead;
        Node cur = pHead;
        while(cur!=null){
            if(cur.next!=null && cur.next.value==cur.value){
                while(cur.next!=null && cur.next.value==cur.value){
                    cur = cur.next;
                }
                cur = cur.next;
                newHead.next = cur;

            }else {
                newHead.next = cur;
                newHead =  cur;
                cur = cur.next;
            }
        }
        return pre.next;
    }

    /**
     * 在一个排序的链表中，存在重复的结点，请重复的结点不保留，返回链表头指针，删除该链表中重复的结点，。 例如，
     * list
     * 链表1->2->2->2->2->3->3->4->4->5 处理后为 1->2->5
     */

    public static Node deleteRepeteNode(Node head){
        if(head==null || head.next ==null){
            return  head;
        }
        Node newHeand = new Node(-1);

        Node pre = newHeand;

        Node cur = head;
        while(cur!=null){
            if(cur.next!=null && cur.value==cur.next.value){
                while(cur.next!=null && cur.next.value == cur.value){
                    cur = cur.next;
                }
                cur = cur.next;
                newHeand.next = cur;
            }else {
                newHeand.next = cur;
                newHeand = cur;
                cur = cur.next;
            }
        }
        return pre.next;
    }


}
